﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using deckAnalyser.Support;
using deckAnalyser.Domain;

namespace deckAnalyser.Controls
{
  public partial class DeckViewControl : UserControl
  {
    private Deck deck = null;
    public Deck Deck
    {
      get { return deck; }
      set
      {
        if (deck != null)
          deck.OnUpdate -= OnDeckUpdate;

        deck = value;

        if (deck != null)
          deck.OnUpdate += OnDeckUpdate;

        UpdateGrid();
      }
    }

    void OnDeckUpdate(DomainObject d)
    {
      UpdateGrid();
    }

    public String CurrentCard
    {
      get
      {
        DataGridViewRow currentRow = dgv_current.CurrentRow;

        if (currentRow != null)
          return currentRow.Cells["Card Name"].Value as String;

        return null;
      }
    }



    public DeckViewControl()
    {
      InitializeComponent();

      dgv_maindeck.CellDoubleClick += dgv_CellDoubleClick;
      dgv_sideboard.CellDoubleClick += dgv_CellDoubleClick;

      tabControl.SelectedIndexChanged += tabControl_SelectedIndexChanged;

      dgv_current = dgv_maindeck;
    }

    DataGridView dgv_current;
    void tabControl_SelectedIndexChanged(object sender, EventArgs e)
    {
      if (tabControl.SelectedIndex == 0)
        dgv_current = dgv_maindeck;
      else
        dgv_current = dgv_sideboard;
    }

    void dgv_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
      DataGridView source = sender as DataGridView;

      if (source["Object", e.RowIndex].Value is Card)
      {
      }
      else
      {
        int x = e.RowIndex + 1;
        source["Card name", e.RowIndex].Value = (source.Rows[x].Visible ? "[+] " : "[-] ") + (source["Card name", e.RowIndex].Value as String).Remove(0, 4);
        while (x < source.Rows.Count && source["Object", x].Value is Card)
        {
          source.Rows[x].Visible = source.Rows[x].Visible ? false : true;
          x++;
        }
      }
    }

    private void buildGrid(DataGridView dgv, DataTable dt)
    {
      dgv.DataSource = dt;

      dgv.AllowUserToAddRows = false;
      dgv.AllowUserToDeleteRows = false;
      dgv.MultiSelect = false;
      dgv.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
      dgv.MultiSelect = false;

      dgv.Columns["Card Name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
      dgv.Columns["Need"].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
      dgv.Columns["Count"].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;

      dgv.Columns["Object"].Visible = false;

      for (int i = 0; i < dt.Columns.Count; i++)
      {
        dgv.Columns[i].ReadOnly = true;
      }

      System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
      System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();

      dataGridViewCellStyle1.ForeColor = System.Drawing.Color.Black;
      dataGridViewCellStyle1.SelectionBackColor = System.Drawing.Color.LightGray;
      dataGridViewCellStyle1.SelectionForeColor = System.Drawing.Color.Black;
      dgv.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle1;

      dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
      dataGridViewCellStyle2.BackColor = System.Drawing.SystemColors.Window;
      dataGridViewCellStyle2.Font = new System.Drawing.Font("Microsoft Sans Serif", 7.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
      dataGridViewCellStyle2.ForeColor = System.Drawing.Color.Black;
      dataGridViewCellStyle2.SelectionBackColor = System.Drawing.Color.LightGray;
      dataGridViewCellStyle2.SelectionForeColor = System.Drawing.Color.Black;
      dataGridViewCellStyle2.WrapMode = System.Windows.Forms.DataGridViewTriState.False;
      dgv.DefaultCellStyle = dataGridViewCellStyle2;

      dgv.RowEnter += dataGridView_RowEnter;
      dgv.RowPrePaint += dataGridView_RowPrePaint;
    }


    public void UpdateGrid()
    {
      if (deck == null) return;

      buildGrid(dgv_maindeck, deck.ToDataTable(true));
      buildGrid(dgv_sideboard, deck.ToDataTable(false));
    }

    public void Clear()
    {
    }

    private CardDetailsControl cardDetailsControl = null;
    public CardDetailsControl CardDetailsControl
    {
      get { return cardDetailsControl; }
      set { cardDetailsControl = value; }
    }

    //private EditorControl editorControl = null;
    //public EditorControl EditorControl
    //{
    //  get { return editorControl; }
    //  set { editorControl = value; }
    //}

    private void dataGridView_RowEnter(object sender, DataGridViewCellEventArgs e)
    {
      DataGridView source = sender as DataGridView;

      UpdateCardDetailsControl(e.RowIndex);

      //if (editorControl != null)
      //{
      //  editorControl.Source = (String)source["Card Name", e.RowIndex].Value;
      //}
    }

    private void dataGridView_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
    {
      DataGridView source = sender as DataGridView;

      if (source["Object", e.RowIndex].Value is Card)
      {
        Card card = source["Object", e.RowIndex].Value as Card;

        int need = (int)(source.Rows[e.RowIndex].Cells["Need"].Value);

        if (need > 0)
        {
          source.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Red;
          source.Rows[e.RowIndex].DefaultCellStyle.SelectionForeColor = Color.Red;
        }
        else
        {
          source.Rows[e.RowIndex].Cells[0].Style.ForeColor = Color.Transparent;
          source.Rows[e.RowIndex].Cells[0].Style.SelectionForeColor = Color.Transparent;
        }

      }
      else
      {
        source.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.SteelBlue;
        source.Rows[e.RowIndex].DefaultCellStyle.Font = new Font(FontFamily.GenericSansSerif, 10);
      }
    }

    private void UpdateCardDetailsControl(int row)
    {
      if (row < dgv_current.Rows.Count)
      {
        if (cardDetailsControl != null)
        {
          if (dgv_current["Object", row].Value is Card)
          {
            Card card = dgv_current["Object", row].Value as Card;
            cardDetailsControl.CardName = card.Name;
            cardDetailsControl.CardCost = card.Cost;
            cardDetailsControl.CardType = card.Type;
            cardDetailsControl.CardSet = "";
            cardDetailsControl.CardRarity = card.Rarity;
            cardDetailsControl.CardText = card.Text;
          }
        }
      }
    }


  }
}
